package com.cskaoyan.service.wx;/**
 * @author mj
 * @date 2023-01-02 00:43
 * @describe
 */

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketAddress;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.mapper.marketMapper.AdminAddressMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author MJ
 * @date 2023-01-02 00:43
 * @describe
 */
@Transactional
@Service
public class WXAddressServiceImpl implements WXAddressService {
    @Autowired
    AdminAddressMapper adminAddressMapper;
    @Override
    public CommonData<MarketAddress> list(CommonData<MarketAddress> marketAddressCommonData) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        Integer userId = principal.getId();
        List<MarketAddress> addressList = adminAddressMapper.selectAddress(userId);

        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(addressList);
        CommonData data = CommonData.data(marketAddressPageInfo);

        return data;
    }

    @Override
    public int userAddressCreate(MarketAddress param) {

        adminAddressMapper.insertAddress(param);

        return 1;
    }

    @Override
    public MarketAddress selectAddress(String id) {
        MarketAddress result = adminAddressMapper.selectAddressById(id);
        result.setUpdateTime(new Date());
        return result;
    }

    @Override
    public MarketAddress userAddressUpdate(MarketAddress param) {
        adminAddressMapper.updateAddress(param);
        MarketAddress result =adminAddressMapper.selectAddressById(param.getId().toString());
        return result;
    }

    @Override
    public void deleteAddress(Integer id) {
        adminAddressMapper.deleteAddress(id);
    }
}
